package util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import dominio.padrao.Padrao;

public class PatternFileParser {
	
	/**
	 * 
	 * @return
	 */
	public static List<Padrao> getPadroesTreinamento( String fileName, int qtdePadroes, int qtdeAtributos ) {
		
		List<Padrao> padroesTreinamento = new ArrayList<Padrao>();
		
		InputStream in = PatternFileParser.class.getResourceAsStream( fileName );
		BufferedReader bf = new BufferedReader( new InputStreamReader( in ) );
		
		try {
			double[][] aux = new double[qtdePadroes][qtdeAtributos];
			
			int countLinhaArquivo = 0;
			
			while(bf.ready()){
				String linha = bf.readLine();
				
				while( linha.contains( "  " ) ){
					linha = linha.replaceAll( "\\s\\s", " " );
				}
				linha = linha.trim();
				
				String[] numsLinha = linha.split( " " );
				for( int i = 0; i < numsLinha.length; i++ ) {
					String num = numsLinha[ i ];
					aux[i][countLinhaArquivo] = Double.parseDouble( num );
				}
				
				countLinhaArquivo++;
			}
			
			bf.close();
			bf = null;
			
			for( int i = 0; i < aux.length; i++ ) {
				double[] atributos = aux[ i ];
				padroesTreinamento.add( new Padrao( atributos ) );
			}
			
		} catch ( IOException e ) {
			e.printStackTrace();
		}
		
		return padroesTreinamento;
	}

}
